package com.jadows.tdog.controller;

import com.jadows.tdog.base.R;
import com.jadows.tdog.dto.UserInfo;
import com.jadows.tdog.po.RecentContactPo;
import com.jadows.tdog.service.ContactService;
import com.jadows.tdog.util.SecurityUtil;
import com.jadows.tdog.util.UnionUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 最近联系人列表
 *
 * @author taojiacheng
 */
@RestController
@RequestMapping("/recent")
@RequiredArgsConstructor
public class ContactController {

    final ContactService contactService;

    /**
     * 获取最近联系人列表
     */
    @GetMapping("/list")
    public R<?> list() {
        return R.ok(contactService.listRecentContacts());
    }

    /**
     * 创建最近联系人
     */
    @PostMapping("/add")
    public R<?> createRecentContact(@RequestBody RecentContactPo recentContactPo) {
        UserInfo currentUser = SecurityUtil.getCurrentUser();
        String unionId = UnionUtil.getUnionId(currentUser.getMemberId(), recentContactPo.getTargetId());
        recentContactPo.setUnionId(unionId);
        return R.ok(contactService.createRecentContactIfAbsent(recentContactPo));
    }

    /**
     * 获取最近联系人信息
     */
    @GetMapping("/info")
    public R<?> getRecentContactInfo(RecentContactPo recentContactPo) {
        UserInfo currentUser = SecurityUtil.getCurrentUser();
        String unionId = UnionUtil.getUnionId(currentUser.getMemberId(), recentContactPo.getTargetId());
        recentContactPo.setUnionId(unionId);
        return R.ok(contactService.createRecentContactIfAbsent(recentContactPo));
    }


    @PostMapping("/update")
    public R<?> updateRecentContact(@RequestBody RecentContactPo recentContactPo) {
        UserInfo currentUser = SecurityUtil.getCurrentUser();
        String unionId = UnionUtil.getUnionId(currentUser.getMemberId(), recentContactPo.getTargetId());
        recentContactPo.setUnionId(unionId);
        contactService.updateRecentContact(recentContactPo);
        return R.ok();
    }

    /**
     * 屏蔽最近联系人
     */
    @PostMapping("/block")
    public R<?> blockRecentContact(@RequestBody RecentContactPo recentContactPo) {
        contactService.blockRecentContact(recentContactPo);
        return R.ok();
    }

    /**
     * 取消屏蔽最近联系人
     */
    @PostMapping("/unblock")
    public R<?> unblockRecentContact(@RequestBody RecentContactPo recentContactPo) {
        contactService.unblockRecentContact(recentContactPo);
        return R.ok();
    }

    /**
     * 删除最近联系人
     */
    @PostMapping("/delete")
    public R<?> deleteRecentContact(@RequestBody RecentContactPo recentContactPo) {
        contactService.deleteRecentContact(recentContactPo);
        return R.ok();
    }


    /**
     * 当前用户屏蔽的联系人列表
     */
    @GetMapping("/block/list")
    public R<?> listBlockContacts() {
        return R.ok(contactService.listBlockContacts());
    }


    /**
     * 内部接口
     */
    @PostMapping("/out")
    public R<Integer> recentOut() {
        return R.ok(contactService.recentOut());
    }
}
